package com.usian.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.article.mapper.ApAuthorMapper;
import com.usian.article.service.ApAuthorService;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ApAuthorServiceImpl extends ServiceImpl<ApAuthorMapper, ApAuthor> implements ApAuthorService {


    @Override
    public ApAuthor findByUserId(Integer id) {

        if (id==null){
            return null;
        }

        QueryWrapper<ApAuthor> wrapper = new QueryWrapper();

        wrapper.lambda().eq(ApAuthor::getUserId,id);

        List<ApAuthor> list = list(wrapper);

        if (list.size()==1){

            return list.get(0);
        }

        return null;
    }

    @Override
    public ResponseResult add(ApAuthor apAuthor) {

        if (apAuthor==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }

        boolean save = save(apAuthor);

        if (save){
            return ResponseResult.okResult(save);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }
}
